{
  "cells": [
    {
      "attachments": {},
      "cell_type": "markdown",
      "metadata": {
        "id": "hNbFoillfVmR"
      },
      "source": [
        "[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/pinecone-io/examples/blob/master/learn/generation/chatbots/nemo-guardrails/02-colang-actions.ipynb) [![Open nbviewer](https://raw.githubusercontent.com/pinecone-io/examples/master/assets/nbviewer-shield.svg)](https://nbviewer.org/github/pinecone-io/examples/blob/master/learn/generation/chatbots/nemo-guardrails/02-colang-actions.ipynb)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 1,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "pZ8GfhOHfVmS",
        "outputId": "af2f695d-f574-4ba6-8461-71e898025f75"
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "\u001b[2K     \u001b[90m\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u001b[0m \u001b[32m13.9/13.9 MB\u001b[0m \u001b[31m8.5 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
            "\u001b[2K     \u001b[90m\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u001b[0m \u001b[32m73.6/73.6 kB\u001b[0m \u001b[31m4.7 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
            "\u001b[2K     \u001b[90m\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u001b[0m \u001b[32m1.0/1.0 MB\u001b[0m \u001b[31m36.9 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
            "\u001b[2K     \u001b[90m\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u001b[0m \u001b[32m1.4/1.4 MB\u001b[0m \u001b[31m51.1 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
            "\u001b[2K     \u001b[90m\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u001b[0m \u001b[32m1.2/1.2 MB\u001b[0m \u001b[31m26.1 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
            "\u001b[2K     \u001b[90m\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u001b[0m \u001b[32m647.5/647.5 kB\u001b[0m \u001b[31m27.6 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
            "\u001b[?25h  Preparing metadata (setup.py) ... \u001b[?25l\u001b[?25hdone\n",
            "\u001b[2K     \u001b[90m\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u001b[0m \u001b[32m86.0/86.0 kB\u001b[0m \u001b[31m9.9 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
            "\u001b[?25h  Preparing metadata (setup.py) ... \u001b[?25l\u001b[?25hdone\n",
            "\u001b[2K     \u001b[90m\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u001b[0m \u001b[32m57.1/57.1 kB\u001b[0m \u001b[31m4.0 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
            "\u001b[2K     \u001b[90m\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u001b[0m \u001b[32m67.0/67.0 kB\u001b[0m \u001b[31m5.5 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
            "\u001b[2K     \u001b[90m\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u001b[0m \u001b[32m58.3/58.3 kB\u001b[0m \u001b[31m5.0 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
            "\u001b[2K     \u001b[90m\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u001b[0m \u001b[32m71.5/71.5 kB\u001b[0m \u001b[31m6.2 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
            "\u001b[2K     \u001b[90m\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u001b[0m \u001b[32m69.6/69.6 kB\u001b[0m \u001b[31m7.1 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
            "\u001b[2K     \u001b[90m\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u001b[0m \u001b[32m90.0/90.0 kB\u001b[0m \u001b[31m9.4 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
            "\u001b[2K     \u001b[90m\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u001b[0m \u001b[32m7.4/7.4 MB\u001b[0m \u001b[31m30.3 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
            "\u001b[2K     \u001b[90m\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u001b[0m \u001b[32m1.3/1.3 MB\u001b[0m \u001b[31m60.2 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
            "\u001b[2K     \u001b[90m\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u001b[0m \u001b[32m268.8/268.8 kB\u001b[0m \u001b[31m22.1 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
            "\u001b[2K     \u001b[90m\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u001b[0m \u001b[32m58.3/58.3 kB\u001b[0m \u001b[31m4.6 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
            "\u001b[2K     \u001b[90m\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u001b[0m \u001b[32m49.4/49.4 kB\u001b[0m \u001b[31m2.7 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
            "\u001b[2K     \u001b[90m\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u001b[0m \u001b[32m7.8/7.8 MB\u001b[0m \u001b[31m120.2 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
            "\u001b[2K     \u001b[90m\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u001b[0m \u001b[32m1.3/1.3 MB\u001b[0m \u001b[31m61.0 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
            "\u001b[?25h  Building wheel for annoy (setup.py) ... \u001b[?25l\u001b[?25hdone\n",
            "  Building wheel for sentence-transformers (setup.py) ... \u001b[?25l\u001b[?25hdone\n",
            "\u001b[31mERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts.\n",
            "ipython 7.34.0 requires jedi>=0.16, which is not installed.\n",
            "cvxpy 1.3.2 requires setuptools>65.5.1, but you have setuptools 65.5.1 which is incompatible.\u001b[0m\u001b[31m\n",
            "\u001b[0m"
          ]
        }
      ],
      "source": [
        "!pip install -qU \\\n",
        "    nemoguardrails==0.4.0 \\\n",
        "    openai==0.27.8"
      ]
    },
    {
      "attachments": {},
      "cell_type": "markdown",
      "metadata": {
        "id": "GZXGTSv2f_Fy"
      },
      "source": [
        "Set your OpenAI API key:"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 2,
      "metadata": {
        "id": "Oc4N2tXxgBMp"
      },
      "outputs": [],
      "source": [
        "import os\n",
        "\n",
        "os.environ[\"OPENAI_API_KEY\"] = os.environ.get(\"OPENAI_API_KEY\") or \"YOUR_API_KEY\""
      ]
    },
    {
      "attachments": {},
      "cell_type": "markdown",
      "metadata": {
        "id": "WuVyO37cfVmT"
      },
      "source": [
        "# Adding Actions\n",
        "\n",
        "In Guardrails we can add _actions_ to our colang files. These actions allow us to execute code, for example:\n",
        "\n",
        "```\n",
        "define flow\n",
        "    user ask question\n",
        "    $answer = execute qa_func(prompt=$last_user_message)\n",
        "    bot $answer\n",
        "```\n",
        "\n",
        "In this Colang flow we expect the user to ask some question (`user ask question`), if so we run an _action_ (equivalent to a Python function) called `qa_func`, we also pass the `$last_user_message` (a default variable set by Guardrails) to this function via the function's `prompt` parameter.\n",
        "\n",
        "The function/action returns a value which we store in the `$answer` context variable, we then tell the bot to return this answer to the user (`bot $answer`).\n",
        "\n",
        "Let's take a look at an example colang file for this:"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 3,
      "metadata": {
        "id": "F41pofCJfVmT"
      },
      "outputs": [],
      "source": [
        "colang_content = \"\"\"\n",
        "# define limits\n",
        "define user ask politics\n",
        "    \"what are your political beliefs?\"\n",
        "    \"thoughts on the president?\"\n",
        "    \"left wing\"\n",
        "    \"right wing\"\n",
        "\n",
        "define bot answer politics\n",
        "    \"I'm a shopping assistant, I don't like to talk of politics.\"\n",
        "\n",
        "define flow politics\n",
        "    user ask politics\n",
        "    bot answer politics\n",
        "    bot offer help\n",
        "\n",
        "# here we use the chatbot for anything else\n",
        "define flow\n",
        "    user ...\n",
        "    $answer = execute response(inputs=$last_user_message)\n",
        "    bot $answer\n",
        "\"\"\"\n",
        "yaml_content = \"\"\"\n",
        "models:\n",
        "- type: main\n",
        "  engine: openai\n",
        "  model: text-davinci-003\n",
        "\"\"\""
      ]
    },
    {
      "attachments": {},
      "cell_type": "markdown",
      "metadata": {
        "id": "83ZFNkebfVmT"
      },
      "source": [
        "Here we have our typical guardrail for any politcal talk. Anything else will get captured by the _catch all_ statement of `user ...` \u2014 triggering the final flow.\n",
        "\n",
        "Within that final flow we execute the `response` function and pass the `last_user_message` via the `inputs` parameter. By itself, this won't do anything, we need to tell Guardrails what this action/function is. Let's begin by initializing our rails."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 4,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 465,
          "referenced_widgets": [
            "3c9445850d9a42e39a20eb52e45fda25",
            "0f07c673025b414cbe3df4e070b8718a",
            "13b80473797d40dbaea66f90d44fbfe2",
            "f5994a43a730411caebbcff5f6a32bd2",
            "d654b56d4bd8406f802462ad6ee7fb55",
            "afd01032526f4012929e4a6050c3bdc8",
            "4996cb8b518a4155a79487c748616b40",
            "7a2650a0657345eea3a89fd94147eb1a",
            "891d96f88ba8409482906197158ba352",
            "6c3292fbd90c43cc95cf5c1d3f65d310",
            "f7101b7c8abe4c57a4a920fb11c9adea",
            "294b3f0ebc5843afb7c9e5e65823c15d",
            "d4eefb2c07be4c1f9951069f0ee2df92",
            "09e6766e0f404d2dbcb3e91ee8ab81b6",
            "77fab2cd19a34cad850262954ab3c071",
            "d21edba65df849f4af527612740278a4",
            "407bbbfe0662450cb2952be3d7d990fa",
            "06c66e2009e54fe1babd87d960fdaf2a",
            "cb71ee734c5e479d8e798216ddab263e",
            "22fe654db5e142bb8466315453f3da43",
            "ab0989b56d0b47c192d560760c6d8e62",
            "a5a44722212842c8ae35588c1ea0b55a",
            "099575372143429fb1186541f8743724",
            "8e6ff164c23047009ba62263613dfd08",
            "90abfeb8f1584afeac77f55636aa150e",
            "16e8294fb85b4fce9d80f854647ad679",
            "0f6cbf17367344e6be02930b00cd8152",
            "44e643f728eb446eb5627c804f433f31",
            "d120992610914e80a6d19f2b659438f5",
            "1df0fb730ca740be8360f6543714918d",
            "6360d21c6b19440cb7434deff0a66f50",
            "b56e339ede4749e385de0a5988a46709",
            "1e929dcbe1cb4b01acabeb4902e8e444",
            "48adb12828024e2c8aa7b7dc1d67a813",
            "ff8801480dc24447a24f49e029aebd35",
            "a9fb65f109f84dd5913fdee08fd45dad",
            "4796db6e9f954430ad32134ddf9d227a",
            "668c9faf32c34199ae673e9194fbe6e0",
            "1a39ae7ba2964fdea48feb9a1218ef40",
            "2371d700661c450fb2379fb82b3895e5",
            "08b592e926d2434a8d8d97b753262aa7",
            "fd81ba0677814612ad00472ec37f6712",
            "457dfed83d954037815c428e72aab6b4",
            "4330cc740dc04b0b97a9cb8d6ee9e0ee",
            "dd197845fd804fd8a6818856e8811612",
            "7c3c1ba686cf42f7b3fef2595a12f97b",
            "af3e5d69126c4472954d626dc9160d7f",
            "068c598a275b48019113b82d059ede4b",
            "0cea0824e4c440ac9973ef5d31b13dea",
            "d8963c19afcf4ff9a02a04c59604c26a",
            "75aa1ad4363f42508ed57a9839795465",
            "d7f545eb223b414e94ca839792718bab",
            "bb357648e8ef444bb0822167c03d1070",
            "5f92b9254afd44e1a8dc00e8a0fa2db6",
            "7a9c4a788c6149069726920afb81700a",
            "c9ad2efc87a54235afc34f24190959f0",
            "7821745ab2184fd6b1a5e485fd33576c",
            "338cd7944a9842e1b373367c8eab23ad",
            "2a7e4d729727405187b22170f9808f2c",
            "dce9e86c90c14d528e16200fc1164bcd",
            "11500d7a54f64665b2f5c498f0fbc618",
            "b914f1be623041f5b5d2bbf8257e1045",
            "f7cc646245ee4430b180b088989aba6a",
            "bbdf3e6c89af42f8a21fd60ecd903964",
            "0b976f2865e440e38fd0ba74d210a294",
            "9fecb9e2df3441c88b5338b5460e0e22",
            "6a8830abf75c49f897668f66d7a49a15",
            "e54668e5913f49308f305b6fe1c60fd7",
            "5dd393f77cd6417c9bf0a30c4dba886f",
            "a7eea28660884fa79caf396c63ab9319",
            "1bf1d876cc454af79375fddcaa238ba1",
            "aa5ff75e19794c23bebee0dc63d767d0",
            "bfdffc69454c4645a42ec932d886ede4",
            "24e9930f3f834e10a2b239726dae1c15",
            "5126d99808c342bf88e7b087e7214f4a",
            "81bf3d522f0647dd9fb6a223d0ef60fd",
            "a3d627b0b77b41f0abe9a77d2ceca784",
            "35be187305364f56b6cb88ffdd7596e8",
            "9a34bde275fd4985a631f53252690316",
            "ba06fb869edd4d4e8b7fdc4bb7e49af3",
            "e35a6d108833416ba3d7ebd604e91db8",
            "e9fa91a579ba4c6f9a7bef85d8f0e3f8",
            "e89efdcfb36d46378fc053c7eb890347",
            "a091ebc9e5084625a3896d7430ce784c",
            "f75e7739ac354104b62849ae2e53e722",
            "75cba1e6afa54f84ad738c51b1c3c220",
            "aaa92fa058c24fad9cca3e2157e4d6e6",
            "2e38294eb00b4058be99b2395824e555",
            "b4ddc86ca12b466fb74f5142a2ae6793",
            "45555f9d07c0434eab9de8a522d42fed",
            "346d8864bcf643b88ff32dcb5a95b084",
            "642fd432cd1045b18f38966a507b13e7",
            "4a04c4c5100b41ddbf6260c26d0b7db9",
            "202e4933720f4e228b86e8600e415e0e",
            "80a8e1b968af49439cb831a38fda6c6e",
            "fd10e2cc173d464ca2ff896c39e7895b",
            "18a85a12e09949438c26530238cfcc10",
            "7c4f4d3056aa4fcb839dac45a758ef27",
            "5d5584952d994bc4870943ddbb7b7028",
            "c2b1d172d424477a9c620fbdab0c96d0",
            "6bd91648165745968823a0daa64e289d",
            "c5c18cd1fddf48b09ec29d8012bce5a2",
            "e6d7f488ed1343299daeeb1c17b05191",
            "c26938b50f10400786664f03a33f1cc2",
            "02a7a003c8584b148d8d6144ade72823",
            "f09900f839074423b747fcd1c3763c02",
            "2d94663d83f64ae48dfe8387ba6a73ad",
            "1532c1cd12e9410ba823034a4f519f99",
            "cb693eb03d3b4b2aa43950d304ed984e",
            "ca30296f9370454b92521c07f6349094",
            "ed94bd7f6ac644428e14e7f20cf775a3",
            "4e7f8e8c3f614ae1bb0863ff8eacbe3e",
            "ec47c1476285497e990fdbbbad2f550f",
            "693a0a2bef2a4f6b9948539f51d347db",
            "5d8ae50f54484979be2a2f0ef9f8be75",
            "3140a340078c42498fbdd2b5b60b8378",
            "0c86b3ededd847f3917917e312770283",
            "3a97c44b31c14c929c76b0bae3262f12",
            "db0f7eaf0f844df3b04ff36e4101cf6d",
            "0879a923a4bd409db9c194a177252712",
            "a8811d9d10964b0c86019366f4452f55",
            "db7abc4130574ed18e77042ce7567788",
            "ce634f3eaf9b41c5995c54cf37657e38",
            "583ad3af94b14ce4a88819682056a89e",
            "1cd30330d65f4b4bae123be9eed7378f",
            "25c1aab6ded84ed59abd0ea13fcf5887",
            "262af26f57ef4d1090c0f19000eba7da",
            "83803c8e390044239fe01ae0ed082a35",
            "2f4fcb652e474c878087200d28a45d34",
            "b1e7b59d8a5c4eb79ce8b2540885adfe",
            "4f286bf747c24802b10bbb94c4da9d3f",
            "b7448c9af82b4c6aba8f7be0147745b4",
            "f0ad3047e139434283098ea2680d4c23",
            "b2f112740fb049159e9722f4f7cc8fb6",
            "179f9ec8ec074e799e18349bfa01df5a",
            "3e23d28b32ee479e93a6e54d4de563d5",
            "f47bd42bfc8141a7bbf0b7aca617e905",
            "6cafd102841e4bed8b4557e6fe537f03",
            "046f5daec27b4d37a00bb34c71c72f7c",
            "8ba44c98eb404b9fb8be333624b2ee40",
            "1b9b80bf843d4030997dd6ae9692e3b4",
            "2240235c552445c8976dc2fa16a544d9",
            "29e7205f4e0f4f3ba664b331e4438987",
            "31b3b06d337f4a4393f7e810a7981397",
            "0c8ab82f84944185844726944e593453",
            "39015cb2fa0e476eb5710cf815bbe8ec",
            "9ed66f3716e84f25b102849a921d7a19",
            "57ac7bb0827c4bc283862661a7707656",
            "395b6e23e737436ea32770c58c2d1757",
            "14aba0ad646741809d9c985398affc0c",
            "9834952a3ba5475abd03c941d367889a",
            "b3d359b5fe2043e6a8b28e261078266b",
            "a9b7491f0f144bb88019d0469ecfd6af",
            "9d7be0bce512427ba631afb767b4de2a"
          ]
        },
        "id": "GGEaBLR9fVmU",
        "outputId": "427c7b67-aa58-4560-e43e-90235833d4ba"
      },
      "outputs": [
        {
          "data": {
            "application/vnd.jupyter.widget-view+json": {
              "model_id": "3c9445850d9a42e39a20eb52e45fda25",
              "version_major": 2,
              "version_minor": 0
            },
            "text/plain": [
              "Downloading (\u2026)e9125/.gitattributes:   0%|          | 0.00/1.18k [00:00<?, ?B/s]"
            ]
          },
          "metadata": {},
          "output_type": "display_data"
        },
        {
          "data": {
            "application/vnd.jupyter.widget-view+json": {
              "model_id": "294b3f0ebc5843afb7c9e5e65823c15d",
              "version_major": 2,
              "version_minor": 0
            },
            "text/plain": [
              "Downloading (\u2026)_Pooling/config.json:   0%|          | 0.00/190 [00:00<?, ?B/s]"
            ]
          },
          "metadata": {},
          "output_type": "display_data"
        },
        {
          "data": {
            "application/vnd.jupyter.widget-view+json": {
              "model_id": "099575372143429fb1186541f8743724",
              "version_major": 2,
              "version_minor": 0
            },
            "text/plain": [
              "Downloading (\u2026)7e55de9125/README.md:   0%|          | 0.00/10.6k [00:00<?, ?B/s]"
            ]
          },
          "metadata": {},
          "output_type": "display_data"
        },
        {
          "data": {
            "application/vnd.jupyter.widget-view+json": {
              "model_id": "48adb12828024e2c8aa7b7dc1d67a813",
              "version_major": 2,
              "version_minor": 0
            },
            "text/plain": [
              "Downloading (\u2026)55de9125/config.json:   0%|          | 0.00/612 [00:00<?, ?B/s]"
            ]
          },
          "metadata": {},
          "output_type": "display_data"
        },
        {
          "data": {
            "application/vnd.jupyter.widget-view+json": {
              "model_id": "dd197845fd804fd8a6818856e8811612",
              "version_major": 2,
              "version_minor": 0
            },
            "text/plain": [
              "Downloading (\u2026)ce_transformers.json:   0%|          | 0.00/116 [00:00<?, ?B/s]"
            ]
          },
          "metadata": {},
          "output_type": "display_data"
        },
        {
          "data": {
            "application/vnd.jupyter.widget-view+json": {
              "model_id": "c9ad2efc87a54235afc34f24190959f0",
              "version_major": 2,
              "version_minor": 0
            },
            "text/plain": [
              "Downloading (\u2026)125/data_config.json:   0%|          | 0.00/39.3k [00:00<?, ?B/s]"
            ]
          },
          "metadata": {},
          "output_type": "display_data"
        },
        {
          "data": {
            "application/vnd.jupyter.widget-view+json": {
              "model_id": "6a8830abf75c49f897668f66d7a49a15",
              "version_major": 2,
              "version_minor": 0
            },
            "text/plain": [
              "Downloading pytorch_model.bin:   0%|          | 0.00/90.9M [00:00<?, ?B/s]"
            ]
          },
          "metadata": {},
          "output_type": "display_data"
        },
        {
          "data": {
            "application/vnd.jupyter.widget-view+json": {
              "model_id": "35be187305364f56b6cb88ffdd7596e8",
              "version_major": 2,
              "version_minor": 0
            },
            "text/plain": [
              "Downloading (\u2026)nce_bert_config.json:   0%|          | 0.00/53.0 [00:00<?, ?B/s]"
            ]
          },
          "metadata": {},
          "output_type": "display_data"
        },
        {
          "data": {
            "application/vnd.jupyter.widget-view+json": {
              "model_id": "b4ddc86ca12b466fb74f5142a2ae6793",
              "version_major": 2,
              "version_minor": 0
            },
            "text/plain": [
              "Downloading (\u2026)cial_tokens_map.json:   0%|          | 0.00/112 [00:00<?, ?B/s]"
            ]
          },
          "metadata": {},
          "output_type": "display_data"
        },
        {
          "data": {
            "application/vnd.jupyter.widget-view+json": {
              "model_id": "c2b1d172d424477a9c620fbdab0c96d0",
              "version_major": 2,
              "version_minor": 0
            },
            "text/plain": [
              "Downloading (\u2026)e9125/tokenizer.json:   0%|          | 0.00/466k [00:00<?, ?B/s]"
            ]
          },
          "metadata": {},
          "output_type": "display_data"
        },
        {
          "data": {
            "application/vnd.jupyter.widget-view+json": {
              "model_id": "ed94bd7f6ac644428e14e7f20cf775a3",
              "version_major": 2,
              "version_minor": 0
            },
            "text/plain": [
              "Downloading (\u2026)okenizer_config.json:   0%|          | 0.00/350 [00:00<?, ?B/s]"
            ]
          },
          "metadata": {},
          "output_type": "display_data"
        },
        {
          "data": {
            "application/vnd.jupyter.widget-view+json": {
              "model_id": "db7abc4130574ed18e77042ce7567788",
              "version_major": 2,
              "version_minor": 0
            },
            "text/plain": [
              "Downloading (\u2026)9125/train_script.py:   0%|          | 0.00/13.2k [00:00<?, ?B/s]"
            ]
          },
          "metadata": {},
          "output_type": "display_data"
        },
        {
          "data": {
            "application/vnd.jupyter.widget-view+json": {
              "model_id": "f0ad3047e139434283098ea2680d4c23",
              "version_major": 2,
              "version_minor": 0
            },
            "text/plain": [
              "Downloading (\u2026)7e55de9125/vocab.txt:   0%|          | 0.00/232k [00:00<?, ?B/s]"
            ]
          },
          "metadata": {},
          "output_type": "display_data"
        },
        {
          "data": {
            "application/vnd.jupyter.widget-view+json": {
              "model_id": "31b3b06d337f4a4393f7e810a7981397",
              "version_major": 2,
              "version_minor": 0
            },
            "text/plain": [
              "Downloading (\u2026)5de9125/modules.json:   0%|          | 0.00/349 [00:00<?, ?B/s]"
            ]
          },
          "metadata": {},
          "output_type": "display_data"
        }
      ],
      "source": [
        "from nemoguardrails import LLMRails, RailsConfig\n",
        "\n",
        "# initialize rails config\n",
        "config = RailsConfig.from_content(\n",
        "    colang_content=colang_content,\n",
        "    yaml_content=yaml_content\n",
        ")\n",
        "# create rails\n",
        "rails = LLMRails(config)"
      ]
    },
    {
      "attachments": {},
      "cell_type": "markdown",
      "metadata": {
        "id": "CNQ3Si4ufVmU"
      },
      "source": [
        "Let's try our guardrails..."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 5,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 36
        },
        "id": "wMiR0TfWfVmU",
        "outputId": "a7bf4530-5531-416f-88f9-3aec19e62af4"
      },
      "outputs": [
        {
          "data": {
            "application/vnd.google.colaboratory.intrinsic+json": {
              "type": "string"
            },
            "text/plain": [
              "\"Action 'response' not found.\""
            ]
          },
          "execution_count": 5,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "await rails.generate_async(prompt=\"hello\")"
      ]
    },
    {
      "attachments": {},
      "cell_type": "markdown",
      "metadata": {
        "id": "6gvMEBMWfVmU"
      },
      "source": [
        "The `response` action couldn't be found. That makes sense we haven't defined anything yet, so let's initialize a function to act as our `response` action."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 13,
      "metadata": {
        "id": "g50VIeJbfVmU"
      },
      "outputs": [],
      "source": [
        "async def func(inputs: str):\n",
        "    return \"hello you are an orange\""
      ]
    },
    {
      "attachments": {},
      "cell_type": "markdown",
      "metadata": {
        "id": "UAmotuvRfVmU"
      },
      "source": [
        "This won't do anything other than tell us we're oranges, but that's fine for this example. Guardrails still doesn't know that this is the correct action to use though:"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 7,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 36
        },
        "id": "zqOhtmgofVmU",
        "outputId": "69a6e4ab-8ae3-4668-9e2e-bf346b534a36"
      },
      "outputs": [
        {
          "data": {
            "application/vnd.google.colaboratory.intrinsic+json": {
              "type": "string"
            },
            "text/plain": [
              "\"Action 'response' not found.\""
            ]
          },
          "execution_count": 7,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "await rails.generate_async(prompt=\"hello\")"
      ]
    },
    {
      "attachments": {},
      "cell_type": "markdown",
      "metadata": {
        "id": "Dkzl4ZU7fVmU"
      },
      "source": [
        "That's because we need to register the action like so:"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 14,
      "metadata": {
        "id": "B8jjXe3MfVmU"
      },
      "outputs": [],
      "source": [
        "rails.register_action(action=func, name=\"response\")"
      ]
    },
    {
      "attachments": {},
      "cell_type": "markdown",
      "metadata": {
        "id": "yRVLJc3yfVmV"
      },
      "source": [
        "Now we can try again:"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 15,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 36
        },
        "id": "bgRfARA2fVmV",
        "outputId": "6aaf45e4-8edf-434b-ea7c-11f9adaefcc8"
      },
      "outputs": [
        {
          "data": {
            "application/vnd.google.colaboratory.intrinsic+json": {
              "type": "string"
            },
            "text/plain": [
              "'hello you are an orange'"
            ]
          },
          "execution_count": 15,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "await rails.generate_async(prompt=\"hello\")"
      ]
    },
    {
      "attachments": {},
      "cell_type": "markdown",
      "metadata": {
        "id": "bn4w59GbfVmV"
      },
      "source": [
        "Perfect, that's the essentials behind using actions in Guardrails. One thing to note here is that we used `async def` to define our action function. If using async Guardrails we do need this otherwise we'll raise a async/await error in Python."
      ]
    }
  ],
  "metadata": {
    "colab": {
      "provenance": []
    },
    "kernelspec": {
      "display_name": "redacre",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.11.3"
    },
    "orig_nbformat": 4
  },
  "nbformat": 4,
  "nbformat_minor": 0
}